package com.longge.wechat.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


import com.longge.wechat.utils.ReadProperties;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrl {
	public static void main(String[] args) throws IOException {
		String ak = ReadProperties.loadProperties().getProperty("AK");
    	
    	/* 根据地理x，y坐标获取位置信息 */
        String locationUrl = "http://api.map.baidu.com/geocoder/v2/?"
        		+ "ak="+ak+"&callback=renderReverse&location=29.2609,120.248&output=json&pois=0";
		
		
		String responseStr = ConnUrl(locationUrl);
		
		responseStr = responseStr.substring(responseStr.indexOf("("));
		responseStr = "["+responseStr.substring(1,responseStr.length()-2)+"]";
		System.out.println(responseStr);
		JSONArray jsonArray = JSONArray.fromObject(responseStr);
		for (int i = 0; i < jsonArray.size(); i++) {
			
			JSONObject jo = JSONObject.fromObject(jsonArray.get(i));
			System.out.println(jo.get("result"));
			JSONArray ja = JSONArray.fromObject(jo.get("result"));
			for (int j = 0; j < ja.size(); j++) {
				JSONObject jo1 = JSONObject.fromObject(ja.get(j));
				System.out.println(jo1.getString("formatted_address"));
			}
			
		}
	}
	
	public static String ConnUrl(String urlStr) throws IOException {
		URL url = new URL(urlStr);
		HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
		//GET Request Define: 
		urlConnection.setRequestMethod("GET");
		urlConnection.connect();
		
		InputStream inputStream = urlConnection.getInputStream();
		
		//Convert Stream to String
		String responseStr = ConvertToString(inputStream);
		return responseStr;
	}
	
	public static String ConvertToString(InputStream inputStream){
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		StringBuilder result = new StringBuilder();
		String line = null;
		try {
			while((line = bufferedReader.readLine()) != null){
				result.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try{
				inputStreamReader.close();
				inputStream.close();
				bufferedReader.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		return result.toString();
	}
	
	public static String ConvertToString(FileInputStream inputStream){
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		StringBuilder result = new StringBuilder();
		String line = null;
		try {
			while((line = bufferedReader.readLine()) != null){
				result.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try{
				inputStreamReader.close();
				inputStream.close();
				bufferedReader.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		return result.toString();
	}

}